@echo off
chcp 65001 >nul

rem 设置变量
set JAVA_HOME=D:\Program Files\jdk-17.0.2
set APP_VERSION=1.1.2
set APP_NAME=EasyShot
set MAIN_JAR=easyshot-java-%APP_VERSION%.jar
set MAIN_CLASS=com.chenju.easyshotjava.core.EasyShot
set ICON_PATH=easyshot.ico

rem 创建日志文件夹和日志文件
set LOG_FILE=package.log
echo ---- Package Log ---- > %LOG_FILE%
echo 日期: %DATE% 时间: %TIME% >> %LOG_FILE%

rem 删除当前目录下的 EasyShot 目录及其内容
if exist "%APP_NAME%" (
  echo 删除当前目录下的 %APP_NAME% 目录及其内容 >> %LOG_FILE%
  rmdir /s /q "%APP_NAME%"
  if %ERRORLEVEL% EQU 0 (
    echo 删除成功 >> %LOG_FILE%
  ) else (
    echo 删除失败，错误代码: %ERRORLEVEL% >> %LOG_FILE%
    echo 删除失败，无法继续执行脚本！
    exit /b
  )
)

rem 选择打包类型
:SELECT_TYPE
echo ===========================================
echo 请选择打包类型:
echo 1. msi
echo 2. exe
echo 3. app-image
echo 0. EXIT
echo ===========================================
set /p TYPE_CHOICE=请输入选项序号 (1/2/3/0):

if "%TYPE_CHOICE%" == "1" (
  set PACKAGE_TYPE=msi
  set PACKAGE_PARAMS=--win-dir-chooser --win-menu --win-shortcut --win-per-user-install
) else if "%TYPE_CHOICE%" == "2" (
  set PACKAGE_TYPE=exe
  set PACKAGE_PARAMS=--win-dir-chooser --win-menu --win-shortcut --win-per-user-install
) else if "%TYPE_CHOICE%" == "3" (
  set PACKAGE_TYPE=app-image
  set PACKAGE_PARAMS=
) else if /i "%TYPE_CHOICE%" == "0" (
  echo 用户选择退出脚本 >> %LOG_FILE%
  echo 已退出脚本！
  exit /b
) else (
  echo 无效输入，请重新选择 >> %LOG_FILE%
  echo 无效输入，请重新选择！
  goto SELECT_TYPE
)

echo 用户选择的打包类型: %PACKAGE_TYPE% >> %LOG_FILE%
echo 已选择: %PACKAGE_TYPE%

rem 创建临时目录
echo mkdir package >> %LOG_FILE%
mkdir package

rem 复制所需文件到临时目录
echo copy to package >> %LOG_FILE%
copy target\%MAIN_JAR% package\ >> %LOG_FILE%

rem 使用jpackage创建可执行文件
echo jpackage cmd >> %LOG_FILE%
"%JAVA_HOME%\bin\jpackage" ^
  --type %PACKAGE_TYPE% ^
  --input package ^
  --dest %APP_NAME% ^
  --name %APP_NAME% ^
  --main-jar %MAIN_JAR% ^
  --main-class %MAIN_CLASS% ^
  --icon %ICON_PATH% ^
  %PACKAGE_PARAMS% ^
  --vendor "ChenJu" ^
  --app-version %APP_VERSION% ^
  --module-path package >> %LOG_FILE% 2>&1

if %ERRORLEVEL% EQU 0 (
  echo 打包成功 >> %LOG_FILE%
  echo 打包成功!
) else (
  echo 打包失败，错误代码: %ERRORLEVEL% >> %LOG_FILE%
  echo 打包失败，错误代码: %ERRORLEVEL%
  goto CLEAN_UP
)

rem 打印生成文件路径
echo 已生成的可执行文件路径: %CD%\%APP_NAME% >> %LOG_FILE%
echo 已生成的可执行文件路径: %CD%\%APP_NAME%

:CLEAN_UP
rem 清理临时目录
echo 清理临时目录 package >> %LOG_FILE%
rmdir /s /q package

echo 操作完成，详情请查看日志文件: %LOG_FILE%

pause